package com.infitecs.eyas.quality;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.judge.service.JudgeService;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: chenkaiyu
 * Date: 15-4-24
 * Time: 下午5:15
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/quality/boom/")
public class BoomController {
    @Autowired
    private BurstService burstService;
    @Autowired
    private JudgeService judgeService;

    @Autowired
    private StepService stepService;

    @RequestMapping(value="saveTheValueOfHGIAndKvtI",method={RequestMethod.POST})
    @ResponseBody
    public AjaxResponse saveTheValueOfHGIAndKvtI(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        String kvTi = String.valueOf(burstService.calculateTheValueOfKvTi(burst));
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
        return new AjaxResponse(kvTi);
    }

    @RequestMapping(value="calculationE50",method={RequestMethod.POST})
    @ResponseBody
    public AjaxResponse calculateKgc(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        String kgc = String.valueOf(burstService.calculateTheValueOfKgc(burst));
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
        return new AjaxResponse(kgc);
    }

    @RequestMapping(value="calculationE51",method={RequestMethod.POST})
    @ResponseBody
    public AjaxResponse calculateHm(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        String hm = String.valueOf(burstService.calculateTheValueOfHm(burst));
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
        return new AjaxResponse(hm);
    }

    @RequestMapping(value="calculationE80",method={RequestMethod.POST})
    @ResponseBody
    public AjaxResponse calculateKd(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        String kd = String.valueOf(burstService.calculateTheValueOfKd(burst));
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
        return new AjaxResponse(kd);
    }

    @RequestMapping(value="saveTheValueOfKe",method={RequestMethod.POST})
    @ResponseBody
    public void saveTheValueOfKe(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        burstService.updateKe(burst);
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
    }

    @RequestMapping(value="saveTheValueOfAi",method={RequestMethod.POST})
    @ResponseBody
    public void saveTheValueOfAi(@RequestBody Burst burst,HttpSession httpSession){
        burst.setPowerCaseId(Integer.parseInt(httpSession.getAttribute("caseId").toString()));
        burstService.updateAi(burst);
        stepService.updateStepStateing(Integer.parseInt(httpSession.getAttribute("caseId").toString()), Constant.STEP_ID_4);
    }

    @RequestMapping(value="save",method={RequestMethod.GET})
    public String save(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        stepService.updateStepStateFinish(caseId,Constant.STEP_ID_4);
        return "redirect:/nature?menuId=14";
    }

    @RequestMapping(value = "recover", method = RequestMethod.GET)
    @ResponseBody
    public Burst getBurst(HttpSession session) {
        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
        return burstService.getBurst(caseId);
    }

    @RequestMapping(value = "boomSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse boomSave(Burst burst) {
        try {
            burstService.boomSave(burst);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }

    @RequestMapping(value = "exportMzfx", method = {RequestMethod.GET, RequestMethod.POST})
    public String exportMzfx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String caseId = request.getParameter("caseId");
        burstService.exportMzfx(request, response, caseId);
        return null;
    }
}
